import { Injectable } from '@nestjs/common';
import { CreateInterfaceDto } from './dto/create-interface.dto';
import { UpdateInterfaceDto } from './dto/update-interface.dto';
import { PrismaService } from '@/prisma/prisma.service';

@Injectable()
export class InterfacesService {
  constructor(private prisma: PrismaService) {}
  async create(dto: CreateInterfaceDto) {
    const yminterface = await this.prisma.interface.create({
      data: {
        //请求的名字
        name: dto.name,
        //请求的类型
        requestType: dto.requestType,
        //请求的链接
        requestUrl: dto.requestUrl,
        //请求头
        requestHeader: JSON.stringify(dto.requestHeader),
        //请求体
        requestBody: JSON.stringify(dto.requestBody),
        //返回结果
        response: JSON.stringify(dto.response),
        //那个目录请求来着
        directoryId: dto.directoryId,
      },
    });
    return yminterface;
  }

  async findAll(directoryId: number) {
    return await this.prisma.interface.findMany({
      where: {
        directoryId,
      },
    });
  }

  async findOne(id: number) {
    return await this.prisma.interface.findFirst({
      where: {
        id,
      },
    });
  }

  async update(id: number, dto: UpdateInterfaceDto) {
    const changeInterface = await this.prisma.interface.update({
      where: {
        id,
      },
      data: {
        //请求的名字
        name: dto.name,
        //请求的类型
        requestType: dto.requestType,
        //请求的链接
        requestUrl: dto.requestUrl,
        //请求头
        requestHeader: JSON.stringify(dto.requestHeader),
        //请求体
        requestBody: JSON.stringify(dto.requestBody),
        //返回结果
        response: JSON.stringify(dto.response),
        //那个目录请求来着
        directoryId: dto.directoryId,
      },
    });
    return changeInterface;
  }

  //删除
  async remove(id: number) {
    return await this.prisma.interface.delete({
      where: {
        id,
      },
    });
  }
}
